﻿Option Explicit On

'*******************************************************************
' Program: pkgVisio
' Author: Albert E Edlund
' Date:
'
' Purpose:
' Working with office object XML
'
'*******************************************************************

#Region " imports "

Imports System.Text
Imports System.Collections
Imports System.Collections.Generic
Imports System.Collections.ObjectModel

Imports System.Xml
Imports System.Linq
Imports System.Linq.Enumerable
Imports System.Windows.Input

Imports Microsoft.Practices.EnterpriseLibrary.Common
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging


' visio v15 does not use a xName prefix which may put it into conflict with other open office apps
Imports <xmlns="http://schemas.microsoft.com/office/visio/2012/main">
' generic namespace for office open xml, used in all of them
Imports <xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">



Imports pkgVisio.clsVisPackage
Imports pkgVisio.clsStructures


#End Region


Public Class frmPages




#Region " Properties and Declarations "

    '
    '
    '
    Public m_xDoc As XDocument
    Public Property xDoc As XDocument
        Get
            Return Me.m_xDoc
        End Get
        Set(value As XDocument)
            If (value Is Nothing) Then
                Throw New System.ArgumentNullException("Value", _
                    "xDoc is Null.")
            End If
            Me.m_xDoc = value
        End Set
    End Property

    '
    '
    '
    Public m_visPackage As clsVisPackage
    Public Property visPackage As clsVisPackage
        Get
            visPackage = Me.m_visPackage
        End Get
        Set(value As clsVisPackage)
            If (value Is Nothing) Then
                Throw New System.ArgumentNullException("Value", _
                    "visPackage is Null.")
            End If
            Me.m_visPackage = value
        End Set
    End Property

    '
    '
    '
    Private m_visPages As clsPages
    Public Property visPages As clsPages
        Get
            visPages = Me.m_visPages
        End Get
        Set(value As clsPages)
            Me.m_visPages = value
        End Set
    End Property

    '
    '
    '
    Private m_visPage As clsPage
    Public Property visPage As clsPage
        Get
            visPage = Me.m_visPage
        End Get
        Set(value As clsPage)
            Me.m_visPage = visPage
        End Set
    End Property




#End Region





#Region " Events "

    '
    '
    '
    Private Sub ExitToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub

    '
    '
    '
    Private Sub AboutToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles AboutToolStripMenuItem.Click
        frmAbout.ShowDialog()
    End Sub

    '
    '
    '
    Private Sub lvPages_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles lvPages.SelectedIndexChanged

        Dim pgItems As ListView.SelectedListViewItemCollection = lvPages.SelectedItems
        Dim pgItem As ListViewItem
        Dim newTopo As clsTopology = Nothing

        Try


            Me.lvEdges.Clear()

            If 0 < pgItems.Count Then

                pgItem = pgItems(0)
                Dim lngPgId As Long = CLng(pgItem.SubItems(0).Text)
                Dim strPgName As String = pgItem.SubItems(1).Text
                Dim strPgNameU As String = pgItem.SubItems(2).Text
                Dim strPgRid As String = pgItem.SubItems(3).Text
                Dim strPgFile As String = pgItem.SubItems(4).Text

                ' make sure we have the pkgpartInfo
                If Me.visPackage.colPkgPartInfo.Count > 0 Then
                    For Each node As pkgpartInfo In Me.visPackage.colPkgPartInfo

                        If node.pkgpartContentType = clsVisPackage.contXmlPage _
                            And node.pkgpartPath = strPgFile _
                            And node.pkgpartParent = "/visio/pages/pages.xml" Then

                            If Me.visPage IsNot Nothing Then
                                Me.visPage = Nothing
                            End If

                            Dim tempDoc As XDocument = Me.visPackage.getPackagePartAsXDoc(Me.visPackage.fileCurrent, node)
                            Me.m_visPage = New clsPage(Me.visPackage, tempDoc)

                            Me.visPage.lngPgId = lngPgId
                            Me.visPage.strPgName = strPgName
                            Me.visPage.strPgNameU = strPgNameU
                            ' get the shapes that are on the page

                            Me.visPackage.loadShapes(tempDoc, lngPgId)
                            Me.visPackage.buildTvShapes(Me.tvShapes, "RootNode")


                            newTopo = New clsTopology(Me.visPackage, lngPgId, tempDoc, CBool(Me.chk1Dgroups.Checked))

                            ' now get the topology 
                            newTopo.loadShapeTopology(Me.lvEdges)

                            Exit For
                        End If ' test for correct node
                    Next node
                End If ' test for current node collection having objects

            End If ' test for something selected

        Catch errXML As XmlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(errXML, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        Catch err As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(err, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

        If newTopo IsNot Nothing Then
            newTopo = Nothing
        End If

    End Sub


    '
    '
    '
    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        Me.m_xDoc = Nothing
        Me.m_xDoc = New XDocument

        Me.m_visPackage = Nothing
        Me.m_visPackage = New clsVisPackage


        Me.lvEdges.Clear()
        Me.lvPages.Clear()

        Me.tvShapes.Nodes.Clear()



    End Sub

    '
    '
    '
    Protected Overrides Sub Finalize()


        If Me.m_xDoc IsNot Nothing Then
            Me.m_xDoc = Nothing
        End If

        If Me.m_visPages IsNot Nothing Then
            Me.m_visPages = Nothing
        End If

        If Me.m_visPage IsNot Nothing Then
            Me.m_visPage = Nothing
        End If


        MyBase.Finalize()
    End Sub

    '
    '
    '
    Private Sub frmPages_Shown(sender As Object, e As System.EventArgs) Handles Me.Shown


        Me.lvEdges.Clear()
        Me.lvPages.Clear()
        Me.tvShapes.Nodes.Clear()

        If Me.visPages IsNot Nothing Then
            Me.m_visPages = Nothing
        End If
        Me.visPages = New clsPages
        Me.visPages.loadPagesXDoc(Me.visPackage)
        Me.visPages.loadPages(lvPages)


    End Sub

    '
    '
    '
    Private Sub tvShapes_AfterSelect(sender As Object, e As System.Windows.Forms.TreeViewEventArgs) Handles tvShapes.AfterSelect

        Try


            Dim newShape As stcShapeInfo = CType(e.Node.Tag, stcShapeInfo)
            Dim tempDoc As XDocument = Nothing


            ' go find a shape with an ID 
            Dim qryShapeInfo = From shpSel In Me.visPage.xDoc...<Shape> _
                Where shpSel.@ID = CStr(newShape.lngShpId)

            If qryShapeInfo.Count = 1 Then
                Using stringReader As New StringReader(qryShapeInfo.First.ToString)
                    tempDoc = XDocument.Load(XmlReader.Create(stringReader))
                End Using
            End If ' test for query count

            If tempDoc IsNot Nothing Then

                Dim newPS As stcPageShape
                With newPS
                    .psPage = newShape.lngPgId
                    .psShape = newShape.lngShpId
                End With

                frmShape.initShapeData("Shape", Me.visPackage, tempDoc, newPS)
                frmShape.strShpName = newShape.strShpName
                frmShape.strShpNameU = newShape.strShpNameU

                frmShape.ShowDialog(Me)

            End If


        Catch errXml As XmlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(errXml, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        Catch err As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(err, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    '
    '
    Private Sub tvShapes_BeforeLabelEdit(sender As Object, e As System.Windows.Forms.NodeLabelEditEventArgs) Handles tvShapes.BeforeLabelEdit

        If (e.Node Is tvShapes.TopNode) Then
            tvShapes.LabelEdit = False
            MsgBox("You are not allowed to edit the top node")
        End If

    End Sub

    '
    '
    '
    Private Sub btnPageSheet_Click(sender As System.Object, e As System.EventArgs) Handles btnPageSheet.Click

        Try

            If Me.visPage IsNot Nothing Then

                Dim qryPageInfo As System.Collections.Generic.IEnumerable(Of System.Xml.Linq.XElement) = Nothing
                ' go look at the page class and get the pageid and then find the page xElement in the pages xdoc
                qryPageInfo = From pgSel In Me.visPages.xDoc...<Page> _
                    Where pgSel.@ID = CStr(visPage.lngPgId)

                Dim tempDoc As XDocument = Nothing

                If qryPageInfo.Count = 1 Then
                    Using stringReader As New StringReader(qryPageInfo.First.ToString)
                        tempDoc = XDocument.Load(XmlReader.Create(stringReader))
                    End Using

                    If tempDoc IsNot Nothing Then

                        Dim newPs As stcPageShape
                        With newPs
                            .psPage = -1
                            .psShape = -1
                        End With
                        frmShape.strShpNameU = visPage.strPgNameU
                        frmShape.initShapeData("PageSheet", Me.visPackage, tempDoc, newPs)
                        frmShape.ShowDialog(Me)

                    End If ' test for tempdoc

                End If ' test for query count


            End If ' test to make sure the page class has been created

        Catch err As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(err, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub



#End Region



End Class